package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.entity.*;
import com.service.IPokemonUserDpService;
import com.service.IPokemonUserSkillService;
import com.service.ISkillOriginService;
import com.service.ITalkPokemonUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WangZhen
 * @since 2021-11-12
 */
@RestController
@RequestMapping("/skill")
public class PokemonUserSkillController {

    @Autowired private ISkillOriginService skillOriginService;
    @Autowired private IPokemonUserSkillService userSkillService;
    @Autowired private ITalkPokemonUserService pokemonUserService;
    @Autowired private IPokemonUserDpService pokemonUserDpService;


    //获取 该精灵的技能列表
@RequestMapping("findUserPokemonSkillList")
    public ResponseMessage findUserPokemonSkillList(Integer pokemonId){

    List<PokemonUserSkill> userSkills = userSkillService.list(new LambdaQueryWrapper<PokemonUserSkill>().eq(PokemonUserSkill::getPokeId,pokemonId));

        List<Integer> skillIds = userSkills.stream().map(s -> s.getSkillId()).collect(Collectors.toList());

    List<SkillOrigin> skillOrigins = skillOriginService.list();
    List<SkillOrigin> userSkillListFinally=new ArrayList<>();
    if (skillIds.size()>0){
        userSkillListFinally = skillOrigins.stream().filter(s -> skillIds.contains(s.getSkillId())).collect(Collectors.toList());
    }
    return new ResponseMessage(ResponseMessage.SUCCESS,"成功",userSkillListFinally);

}

    //获取 技能池列表  除了绝对零度以外，都可以直接在技能池里面设置，需要支付2000DP作为手续费，有些专属技能只有特定精灵能学
      @RequestMapping("getSkillPool")
    public ResponseMessage getSkillPool(){

          List<SkillOrigin> skillOrigins = skillOriginService.list();

          List<SkillOrigin> skillOriginList = skillOrigins.stream().filter(s -> !s.getSkillId().equals(8)).collect(Collectors.toList());

          return new ResponseMessage(ResponseMessage.SUCCESS,"成功",skillOriginList);
      }

      /**设置精灵出战技能--后续版本再改进 先不限制技能数量*/



      //学习技能
      @RequestMapping("learnSkill")
      public ResponseMessage learnSkill(Integer pokemonId,Integer skId,Integer userId){

          PokemonUserDp dpUserOne = pokemonUserDpService.getOne(new LambdaQueryWrapper<PokemonUserDp>().eq(PokemonUserDp::getUserId, userId));
          if (dpUserOne.getDp()<5000){
              return new ResponseMessage(ResponseMessage.FAIL,"喂，你没钱学个p技能");
          }
          List<PokemonUserSkill> userSkills = userSkillService.list(new LambdaQueryWrapper<PokemonUserSkill>().eq(PokemonUserSkill::getPokeId,pokemonId));
          List<Integer> skillIds = userSkills.stream().map(s -> s.getSkillId()).collect(Collectors.toList());
          if (skillIds!=null&&skillIds.contains(skId)){
              return new ResponseMessage(ResponseMessage.FAIL,"该技能已经学过了");
          }

          PokemonUserSkill pokemonUserSkill=new PokemonUserSkill();
          pokemonUserSkill.setPokeId(pokemonId);
          pokemonUserSkill.setSkillId(skId);

          //先判断属性是否符合技能学习条件，一般属性的技能谁都可以学
          TalkPokemonUser userPokemonOne = pokemonUserService.getOne(new LambdaQueryWrapper<TalkPokemonUser>().eq(TalkPokemonUser::getId, pokemonId));
          SkillOrigin skillOriginOne = skillOriginService.getOne(new LambdaQueryWrapper<SkillOrigin>().eq(SkillOrigin::getSkillId, skId));
          //这一块处理精灵专属技能逻辑
          if (skId.equals(13)){//裂空座的专属
              if (!userPokemonOne.getPokemonId().equals(13)){
                  return new ResponseMessage(ResponseMessage.FAIL,"这只有裂空座能学哇");
              }
          }



          boolean save=false;

          if (skillOriginOne==null){
              return new ResponseMessage(ResponseMessage.FAIL,"不存在该技能");
          } else if (skillOriginOne.getShuxin().equals(userPokemonOne.getShuxin())){
               save = userSkillService.save(pokemonUserSkill);
          }else if (userPokemonOne.getShuxintwo()!=null&&userPokemonOne.getShuxintwo().equals(skillOriginOne.getShuxin())){
               save = userSkillService.save(pokemonUserSkill);
          }
          else {
              return new ResponseMessage(ResponseMessage.FAIL,"这笨比学不了这个技能哇");
          }
          if (save==true){
              //扣除手续费
              UpdateWrapper wrapper=new UpdateWrapper();
              wrapper.setSql("dp=dp-5000");
              wrapper.eq("user_id",userId);
              pokemonUserDpService.update(wrapper);
          }

          return new ResponseMessage(ResponseMessage.SUCCESS,"学习技能成功",save);

      }


}
